package com.ruoyi.common.utils.sms;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import com.ruoyi.common.config.SmsConfig;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.SmsResult;
public class aliyunSmsUtils {

       public static SmsResult SendSms(String phoneNumber){

              DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", SmsConfig.getAccessKeyId(), SmsConfig.getAccessKeySecret());
              SmsResult smsResult = new SmsResult();
              IAcsClient client = new DefaultAcsClient(profile);
              String randomNumbStr = StringUtils.getRandomCode(6);
              SendSmsRequest request = new SendSmsRequest();
              request.setPhoneNumbers(phoneNumber);//接收短信的手机号码
              request.setSignName(SmsConfig.getSignName());//短信签名名称
              request.setTemplateCode(SmsConfig.getTemplateCode());//短信模板CODE
              request.setTemplateParam("{\"code\":"+randomNumbStr+"}");//短信模板变量对应的实际值

              try {
                     SendSmsResponse  response = client.getAcsResponse(request);
                     smsResult.setSendSmsResponse(response);
                     smsResult.setCode(randomNumbStr);
                     System.out.println(new Gson().toJson(response));
              } catch (ServerException e) {
                     e.printStackTrace();
              } catch (ClientException e) {
                     System.out.println("ErrCode:" + e.getErrCode());
                     System.out.println("ErrMsg:" + e.getErrMsg());
                     System.out.println("RequestId:" + e.getRequestId());
              }

            return smsResult;

       }






}
